package org.ykhl.sdk.appserver;

import org.ykhl.mq.commons.Constants;
import org.ykhl.mq.commons.Utils;
import org.ykhl.mq.commons.receiver.MessageListener;

/**
 * 用于创建{@link MessageQueueKafkaImpl}对象的静态工厂类。
 *
 * Created by Zhangkefei on 2016/1/14.
 */
public class MessageQueueFactoryKafkaImpl {
    private MessageQueueFactoryKafkaImpl(){}

    /**
     * 从证书文件中自动获取authKey, 并创建一个MessageQueue实例.
     *
     * @return
     */
    public static MessageQueueKafkaImpl newInstance() {
        String authKey = Utils.getAuthIdFromKeyStoreUnchecked(
                Constants.KAFKA_SECURITY_KEYSTORE_LOCATION);

        return new MessageQueueKafkaImpl(MessagePusherFactory.newInstance(authKey),
                MessageReceiverFactory.newInstance(authKey));
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param authKey 申请获得的密钥
     * @return
     */
    @Deprecated
    public static MessageQueueKafkaImpl newInstance(String authKey) {
        return new MessageQueueKafkaImpl(MessagePusherFactory.newInstance(authKey),
                MessageReceiverFactory.newInstance(authKey));
    }

    //lww
    @Deprecated
    public static MessageQueueKafkaImpl newMachineInstance(String authKey,String machineIDauthKey) {
        return new MessageQueueKafkaImpl(MessagePusherFactory.newInstance(authKey),
                MessageReceiverFactory.newInstance(machineIDauthKey));
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param serverAddress 连接的平台服务器地址
     * @param authKey 申请获得的密钥
     * @return
     */
    @Deprecated
    public static MessageQueueKafkaImpl newInstance(String serverAddress, String authKey){
        return new MessageQueueKafkaImpl(MessagePusherFactory.newInstance(serverAddress, authKey),
                MessageReceiverFactory.newInstance(serverAddress, authKey));
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param authKey 申请获得的密钥
     * @param listener 推送消息的监听器
     * @return
     */
    @Deprecated
    public static MessageQueueKafkaImpl newInstance(String authKey, MessageListener listener){
        return new MessageQueueKafkaImpl(MessagePusherFactory.newInstance(authKey),
                MessageReceiverFactory.newInstance(authKey, listener));
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param serverAddress 连接的平台服务器地址
     * @param authKey 申请获得的密钥
     * @param listener 推送消息的监听器
     * @return
     */
    @Deprecated
    public static MessageQueueKafkaImpl newInstance(String serverAddress, String authKey,
                                                    MessageListener listener){
        return new MessageQueueKafkaImpl(MessagePusherFactory.newInstance(serverAddress, authKey),
                MessageReceiverFactory.newInstance(serverAddress, authKey, listener));
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param securityFilePath 指定证书文件的路径
     * @return
     */
    public static MessageQueueKafkaImpl newInstanceWithSecurityFilePath(String securityFilePath){

        return new MessageQueueKafkaImpl(
                MessagePusherFactory.newInstanceWithSecurityFilePath(
                        securityFilePath),
                MessageReceiverFactory.newInstanceWithSecurityFilePath(
                        securityFilePath)
        );
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param securityFilePath 指定证书文件的路径
     * @param listener 推送消息的监听器
     * @return
     */
    public static MessageQueueKafkaImpl newInstanceWithSecurityFilePath(String securityFilePath,
                                                                        MessageListener listener){

        return new MessageQueueKafkaImpl(
                MessagePusherFactory.newInstanceWithSecurityFilePath(
                        securityFilePath),
                MessageReceiverFactory.newInstanceWithSecurityFilePath(
                        securityFilePath, listener)
        );
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param serverAddress 连接的平台服务器地址
     * @param securityFilePath 指定证书文件的路径
     * @return
     */
    public static MessageQueueKafkaImpl newInstanceWithSecurityFilePath(String serverAddress,
                                                                        String securityFilePath){

        return new MessageQueueKafkaImpl(
                MessagePusherFactory.newInstanceWithSecurityFilePath(
                        serverAddress, securityFilePath),
                MessageReceiverFactory.newInstanceWithSecurityFilePath(
                        serverAddress, securityFilePath)
        );
    }

    /**
     * 创建一个MessageQueue实例
     *
     * @param serverAddress 连接的平台服务器地址
     * @param securityFilePath 指定证书文件的路径
     * @param listener 推送消息的监听器
     * @return
     */
    public static MessageQueueKafkaImpl newInstanceWithSecurityFilePath(String serverAddress,
                                                                        String securityFilePath,
                                                                        MessageListener listener){

        return new MessageQueueKafkaImpl(
                MessagePusherFactory.newInstanceWithSecurityFilePath(
                        serverAddress, securityFilePath),
                MessageReceiverFactory.newInstanceWithSecurityFilePath(
                        serverAddress, securityFilePath, listener)
        );
    }
}
